<?php
class Item
{
	public $data;
	public $images;
	public function __construct($tag,$id)
	{
		global $icache,$db;
		if(isset($icache[$tag . $id]))
		{
			$this->data=$icache[$tag . $id]->data;
			$this->images=$icache[$tag . $id]->images;
			return;
		}
		$query_r=array(
			'SELECT'	=> '*',
			'FROM'		=> array(
				'items'	=>'i',
			),
			'WHERE'		=> $db->sql_in_set('tag',array($tag)) .' AND '. $db->sql_in_set('id',array($id)),
		);
		$query=$db->sql_build_query('SELECT',$query_r);
		$result=$db->sql_query($query);
		$this->data=$db->sql_fetchrow($result);
		$this->data['description']=htmlspecialchars_decode($this->data['description']);
		$query_r=array(
			'SELECT'	=> 'filename',
			'FROM'		=> array(
				'item_images'	=>'ii',
			),
			'WHERE'		=> $db->sql_in_set('tag',array($tag)) .' AND '. $db->sql_in_set('id',array($id)),
		);
		$query=$db->sql_build_query('SELECT',$query_r);
		$result=$db->sql_query($query);
		$this->images=array();
		$i=0;
		while($row=$db->sql_fetchrow($result))
		{
			$this->images[$i++]=$row['filename'];
		}
		$icache[$tag . $id] = $this;
	}
	public function Delete($img_path)
	{
		global $db,$root_path;
		foreach($this->images as $image)
		{
			if($image==$img_path)
			{
				$query="
				DELETE FROM item_images 
				WHERE ". $db->sql_in_set('tag',array($this->data['tag'])). "
				AND ". $db->sql_in_set('id',array($this->data['id'])). "
				AND ". $db->sql_in_set('filename',array($image));
				
				$db->sql_query($query);
				unlink($root_path . $image);
				return true;
			}
		}
		return false;
	}
	public function Update($name,$price,$description,$amount,$image=null,$images=array())
	{
		$cat=new Category($this->data['catid']);
		$tag=$this->data['tag'];
		$id=$this->data['id'];
		if($image!=null) unlink($root_path. $this->data['img']);
		$query_r=array(
			'name'			=> $name,
			'price'			=> $price,
			'description'	=> $description,
			'amount'		=> $amount,
			'img'			=> $image!=null?$this->data['img']:Image::Upload($image,$cat->folder,$tag.'-'.$id),
		);
		$query="UPDATE items SET " . $db->sql_buid_array('UPDATE',$query_r);
		if(!$db->query($query)) return false;
		for($i=0;$i<count($images['name']);$i++)
		{
			$img=array();
			foreach($images as $key => $tmp)
			{
				$img[$key]=$tmp[$i];
			}
			$query_r=array(
				'tag'	=> $tag,
				'id'	=> $id,
				'filename'	=> Item::Upload($img,$cat->folder,$tag .'-'. $id),
			);
			$query = 'INSERT INTO item_images ' . $db->sql_build_array('INSERT',$query_r);
			$db->sql_query($query);
		}
	}
	public static function Add($cat_id,$tag,$name,$price,$description,$amount,$image=null,$images=array())
	{
		global $db;
		if($image==null) return false;
		$query_r = array(
			'SELECT'	=> 'id',
			'FROM'		=> array(
				'items'		=>	'i',
			),
			'WHERE'		=> $db->sql_in_set('tag',array($tag)),
			'ORDER_BY'	=> 'id DESC',
		);
		$query = $db->sql_build_query('SELECT',$query_r);
		//echo $query;
		$result = $db->sql_query_limit($query,1);
		$row=$db->sql_fetchrow($result);
		$id=isset($row['id'])?$row['id']+1:1;
        //echo $id;

        $cat=new Category($cat_id);
		$prim_img=Item::Upload($image,$cat->folder,$tag .'-'. $id);
		$query_r = array(
            'tag'    => $tag,
            'id'        => $id,
            'name'      => $name,
            'price'     =>  $price,
            'description'   => $description,
            'amount'        =>  $amount,
            'catid'     => $cat_id,
			'img'		=> $prim_img,
        );
		
        $query = 'INSERT INTO items ' . $db->sql_build_array('INSERT',$query_r);
		if(!$db->sql_query($query)) return false;
		for($i=0;$i<count($images['name']);$i++)
		{
			$img=array();
			foreach($images as $key => $tmp)
			{
				$img[$key]=$tmp[$i];
			}
			$query_r=array(
				'tag'	=> $tag,
				'id'	=> $id,
				'filename'	=> Item::Upload($img,$cat->folder,$tag .'-'. $id),
			);
			$query = 'INSERT INTO item_images ' . $db->sql_build_array('INSERT',$query_r);
			$db->sql_query($query);
		}
	}
    private static function Upload($image,$path,$name)
    {
		global $root_path;
        $extension = strtolower(substr(strrchr($image['name'], '.'), 1));
        if(!($extension=='jpg' || $extension =='png'))
        {
            return '';
        }
        $time = microtime(true);
        $new_image = $path.  $name . '-'. $time . "." . $extension;
        $destination=$root_path. 'images/'.$new_image;
        $action = copy($image['tmp_name'], $destination);
        if($action)
            return 'images/'.$new_image;
        return '';
    }

}
?>